package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;

/**
 * ClassName: PaymentServiceImpl
 * Package: com.atguigu.gmall.payment.service.impl
 * Description:
 *
 * @Author 刘轩
 * @Create 2023-09-05 21:18
 * @Version 1.0
 */

@Service
public class PaymentServiceImpl  implements PaymentService {
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private RabbitService rabbitService;
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
// 查询交易记录是否存在
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfo::getOrderId,orderInfo.getId());
        wrapper.eq(PaymentInfo::getPaymentType,paymentType);
        PaymentInfo paymentInfoQuery = paymentInfoMapper.selectOne(wrapper);
        if (paymentInfoQuery!=null) {
            return;
        }
            // 创建pamentInfo 对象
            PaymentInfo paymentInfo = new PaymentInfo();
            paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
            paymentInfo.setOrderId(orderInfo.getId());
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setPaymentType(paymentType);
            paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
            paymentInfo.setSubject(orderInfo.getTradeBody());
            paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
            paymentInfo.setCallbackTime(new Date());
            paymentInfoMapper.insert(paymentInfo);

    }

    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String paymentType) {
        // 查询数据
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo);
        wrapper.eq(PaymentInfo::getPaymentType,paymentType);
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(wrapper);
        return paymentInfo;
    }


    @Override
    public void upDatePaymentInfoStatus(String outTradeNo, String paymentType, HashMap<String, String> paramsMap) {

        PaymentInfo paymentInfoQuery = this.getPaymentInfo(outTradeNo, paymentType);
        if(paymentInfoQuery == null) return ;
        // 修改交易记录状态
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setTradeNo(paramsMap.get("trade_no"));
        paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
        paymentInfo.setCallbackTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
        paymentInfo.setCallbackContent(paramsMap.toString());
        // 构建条件
//        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(PaymentInfo::getPaymentType,paymentType);
//        wrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo);
//        // 第一个参数：设置修改的数据，第二个数据，设置更新条件
//        paymentInfoMapper.update(paymentInfo,wrapper);
        this.upDatePaymentInfoStatus(outTradeNo,paymentType,paymentInfo);
            // 发送消息给订单，更改订单状态
        rabbitService.sendMsg(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,paymentInfoQuery.getOrderId());

    }

    @Override
    public void upDatePaymentInfoStatus(String outTradeNo, String paymentType, PaymentInfo paymentInfo) {
        // 构建条件
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfo::getPaymentType,paymentType);
        wrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo);
        //更新
        paymentInfoMapper.update(paymentInfo,wrapper);
    }


    /**
     * 关闭交易记录
     * @param orderId
     */
    @Override
    public void closePayment(Long orderId) {
// 创建对象
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.CLOSED.name()); // 交易记录已关闭
        // 设置更新条件
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfo::getOrderId,orderId);
        paymentInfoMapper.update(paymentInfo,wrapper);
    }
}
